package be.rivendale.renderer.mk2.window;

import be.rivendale.renderer.mk2.data.Segment;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Canvas extends JComponent {

    private BufferedImage image;

    public Canvas(int width, int height) {
        setPreferredSize(new Dimension(width, height));
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    }

    public void blit(Segment segment) {
        image.getRaster().setPixels(segment.getRectangle().getX(), segment.getRectangle().getY(), segment.getRectangle().getWidth(), segment.getRectangle().getHeight(), segment.getData());
        repaint();
    }

    @Override
    public void paint(Graphics graphics) {
        graphics.drawImage(image, 0, 0, null);
    }
}
